using System;
using System.Web.Mvc;
using Autofac;
using eCommerce.Core.Infrastructure;
using eCommerce.Core.Infrastructure.DependencyManagement;

namespace eCommerce.Core
{
    public class YiEngine : IEngine
    {


        //private ContainerManager _containerManager;


        //public ContainerManager ContainerManager
        //{
        //    get { return _containerManager; }
        //}

        private static IContainer container;

        public YiEngine()
        {
          

        }
     


        public void Initialize(IContainer containerParameter)
        {
            container = containerParameter;
        }

        public T Resolve<T>() where T : class
        {
            try
            {
                return DependencyResolver.Current.GetService<T>();
                //return AutofacRequestLifetimeHttpModule.GetLifetimeScope(container, null).Resolve<T>();
            }
            catch (Exception e)
            {
                
            }
            return container.Resolve<T>();
        }


        public T ResolveNamed<T>(string name) where T : class
        {
            try
            {
                return AutofacRequestLifetimeHttpModule.GetLifetimeScope(container, null).ResolveNamed<T>(name);
            }
            catch (Exception e)
            {

            }
            return container.ResolveNamed<T>(name);
        }

        public object Resolve(Type type)
        {
            try
            {
                return AutofacRequestLifetimeHttpModule.GetLifetimeScope(container, null).Resolve(type);
            }
            catch (Exception e)
            {

            }

            return container.Resolve(type);
        }

       
    }
}